package com.wanmait.wanmarecruit.webController;

import com.wanmait.wanmarecruit.entity.*;
import com.wanmait.wanmarecruit.service.*;
import org.apache.commons.io.FilenameUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("web")
public class WebPersonalProfileController {
    @Resource
    private ProvinceService provinceService;
    @Resource
    private TechnologyService technologyService;
    @Resource
    private PositionTypeSecondService positionTypeSecondService;
    @Resource
    private WorkExperienceService workExperienceService;
    @Resource
    private EducationExperienceService educationExperienceService;
    @Resource
    private HonorsWonService honorsWonService;
    @Resource
    private StudentCadreExperienceService studentCadreExperienceService;
    @Resource
    private UserService userService;
    @Resource
    private CityService cityService;
    @Resource
    private JobOrientationService jobOrientationService;
    @Resource
    private JobStatusService jobStatusService;

    @PostMapping("/personal/insertProfile")
    @ResponseBody
    public String insertProfile(User user,HttpSession session,MultipartFile facePic,HttpServletRequest request)
    {
        Integer userId = ((User)session.getAttribute("user")).getId();
        user.setId(userId);
        if (facePic!=null)
        {
            if (!facePic.isEmpty())
            {
                String fileNameExe = FilenameUtils.getExtension(facePic.getOriginalFilename());
                String fileName = UUID.randomUUID()+"."+fileNameExe;
                String path = request.getServletContext().getRealPath("/static/uploadImages");
                try {
                    facePic.transferTo(new File(path+"/"+fileName));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                user.setFace(fileName);
            }
        }
        userService.updateById(user);
        session.setAttribute("user",user);
        return user.getFace();
    }

    @GetMapping("/personal/getPositionTypeIdBySecond")
    @ResponseBody
    public String getPositionTypeIdBySecond(Integer positionTypeSecondId)
    {
        return positionTypeSecondService.getById(positionTypeSecondId).getPositionTypeId()+"";
    }

    @PostMapping("/personal/changePositionTypeSecond")
    @ResponseBody
    public List<PositionTypeSecond> changePositionTypeSecond(Integer positionTypeId)
    {
        return positionTypeSecondService.getTypeByPositionTypeId(positionTypeId);
    }

    @PostMapping("/personal/changeCity")
    @ResponseBody
    public List<City> changeCity(Integer provId){
        return cityService.getCityByProvince(provId);
    }

    @GetMapping("/personal/getProvByCityId")
    @ResponseBody
    public String getProvByCityId(Integer cityId)
    {
        return cityService.getProvByCityId(cityId).getProvId()+"";
    }

    @PostMapping("/personal/addTec")
    @ResponseBody
    public String addTec(HttpSession session,Technology technology)
    {
        technology.setUserId(((User)session.getAttribute("user")).getId());
        return technologyService.insertTec(technology)+"";
    }

    @GetMapping("/personal/delOneTec")
    @ResponseBody
    public void delOneTec(Integer id)
    {
        technologyService.removeById(id);
    }

    @PostMapping("/personal/addOri")
    @ResponseBody
    public String addOri(JobOrientation jobOrientation,HttpSession session)
    {
        jobOrientation.setUserId(((User)session.getAttribute("user")).getId());
        jobOrientationService.save(jobOrientation);
        return jobOrientation.getId()+"";
    }

    @PostMapping("/personal/updateOneOri")
    @ResponseBody
    public void updateOneOri(JobOrientation jobOrientation)
    {
        jobOrientationService.updateById(jobOrientation);
    }

    @GetMapping("/personal/delOneOri")
    @ResponseBody
    public void delOneOri(Integer id)
    {
        jobOrientationService.removeById(id);
    }

    @PostMapping("/personal/addExp")
    @ResponseBody
    public String addExp(WorkExperience workExperience,HttpSession session)
    {
        workExperience.setUserId(((User)session.getAttribute("user")).getId());
        workExperienceService.save(workExperience);
        return workExperience.getId()+"";
    }

    @PostMapping("/personal/updateOneExp")
    @ResponseBody
    public void updateOneExp(WorkExperience workExperience)
    {
        workExperienceService.updateById(workExperience);
    }

    @GetMapping("/personal/delOneExp")
    @ResponseBody
    public void delOneExp(Integer id)
    {
        workExperienceService.removeById(id);
    }

    @PostMapping("/personal/addEdu")
    @ResponseBody
    public String addEdu(EducationalExperience educationalExperience,HttpSession session)
    {
        educationalExperience.setUserId(((User)session.getAttribute("user")).getId());
        educationExperienceService.save(educationalExperience);
        return educationalExperience.getId()+"";
    }

    @PostMapping("/personal/updateOneEdu")
    @ResponseBody
    public void updateOneEdu(EducationalExperience educationalExperience)
    {
        educationExperienceService.updateById(educationalExperience);
    }

    @GetMapping("/personal/delOneEdu")
    @ResponseBody
    public void delOneEdu(Integer id)
    {
        educationExperienceService.removeById(id);
    }

    @PostMapping("/personal/addCad")
    @ResponseBody
    public String addCad(StudentCadreExperience studentCadreExperience,HttpSession session)
    {
        studentCadreExperience.setUserId(((User)session.getAttribute("user")).getId());
        studentCadreExperienceService.save(studentCadreExperience);
        return studentCadreExperience.getId()+"";
    }

    @PostMapping("/personal/updateOneCad")
    @ResponseBody
    public void updateOneCad(StudentCadreExperience studentCadreExperience)
    {
        studentCadreExperienceService.updateById(studentCadreExperience);
    }

    @GetMapping("/personal/delOneCad")
    @ResponseBody
    public void delOneCad(Integer id)
    {
        studentCadreExperienceService.removeById(id);
    }

    @PostMapping("/personal/addHor")
    @ResponseBody
    public String addHor(HonorsWon honorsWon,HttpSession session)
    {
        honorsWon.setUserId(((User)session.getAttribute("user")).getId());
        honorsWonService.save(honorsWon);
        return honorsWon.getId()+"";
    }

    @GetMapping("/personal/delOneHor")
    @ResponseBody
    public void delOneHor(Integer id)
    {
        honorsWonService.removeById(id);
    }

    @PostMapping("/personal/previewResume")
    @ResponseBody
    public ModelAndView previewResume(HttpSession session)
    {
        User user = (User)session.getAttribute("user");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("user",userService.getById(user.getId()));
        modelAndView.addObject("provinces",provinceService.getProvincesBySort());
        modelAndView.addObject("jobStatuses",jobStatusService.list());
        modelAndView.addObject("technologies",technologyService.getTecByUser(user.getId()));
        modelAndView.addObject("workExperiences",workExperienceService.getWorkExperienceByUser(user.getId()));
        modelAndView.addObject("educationExperiences",educationExperienceService.getEducationByUser(user.getId()));
        modelAndView.addObject("studentCadreExperiences",studentCadreExperienceService.getCadreByUserId(user.getId()));
        modelAndView.addObject("honorsWons",honorsWonService.getHonorsByUserId(user.getId()));
        modelAndView.addObject("jobOrientations",jobOrientationService.getJobOrientationByUserId(user.getId()));
        return modelAndView;
    }

}
